package com.ysx.server.controller;

import com.ysx.server.pojo.dto.UserLoginDTO;
import com.ysx.server.pojo.dto.UserRegisterDTO;
import com.ysx.server.pojo.entity.Goods;
import com.ysx.server.pojo.entity.User;
import com.ysx.server.pojo.vo.UserInfoVO;
import com.ysx.server.service.GoodsService;
import com.ysx.server.service.UserService;
import com.ysx.server.utils.CaptchaUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.ysx.server.common.content.USER_SESSION;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private GoodsService goodsService;

    /**
     * 注册
     * @param userRegisterDTO
     * @return
     */
    @PostMapping("/register")
    public boolean register(@RequestBody UserRegisterDTO userRegisterDTO, HttpServletRequest request){
        log.info("用户注册:{}",userRegisterDTO);
        if(userRegisterDTO==null){
            return false;
        }
        return userService.register(userRegisterDTO,request);
    }

    /**
     * 获取验证码
     * @return
     */
    @GetMapping("/captcha")
    public String captcha(){
        String captcha = CaptchaUtils.getCaptcha();
        log.info("验证码为：{}",captcha);
        return captcha;
    }

    /**
     * 获取登陆用户的信息
     * @param userLoginDTO
     * @param request
     * @return
     */
    @PostMapping("/login")
    public UserInfoVO login(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request){
        if(userLoginDTO==null){
            return null;
        }
        User user = userService.login(userLoginDTO);
        UserInfoVO userInfoVo = UserInfoVO.builder()
                .id(user.getId())
                .userAccount(user.getUserAccount())
                .username(user.getUsername())
                .avatarUrl(user.getAvatarUrl())
                .isAdmin(user.getIsAdmin())
                .email(user.getEmail())
                .userStatus(user.getUserStatus())
                .createTime(user.getCreateTime())
                .build();
        request.getSession().setAttribute(USER_SESSION,userInfoVo);
        return userInfoVo;
    }

    /**
     * 退出登陆
     * @param request
     * @return
     */
    @GetMapping("/outLogin")
    public boolean outLogin(HttpServletRequest request){
        request.getSession().removeAttribute(USER_SESSION);
        log.info("退出登陆");
        return true;
    }

    @GetMapping("/info")
    public UserInfoVO info(HttpServletRequest request){
        //TODO
        UserInfoVO userInfoVo = (UserInfoVO)request.getSession().getAttribute(USER_SESSION);
        return userInfoVo;
    }

    @GetMapping("/userMsg")
    public UserInfoVO userMsg(HttpServletRequest request){
        UserInfoVO userInfoVo = (UserInfoVO)request.getSession().getAttribute(USER_SESSION);
        log.info("userInfo:{}",userInfoVo);
        return userInfoVo;
    }

    /**
     * 用户商品
     * @param request
     * @return
     */
    @GetMapping("userGoods")
    public List<Goods> userGoods(HttpServletRequest request){
        UserInfoVO userInfoVo = (UserInfoVO)request.getSession().getAttribute(USER_SESSION);
        List<Goods> goodsList = goodsService.getByUserId(userInfoVo.getId());
        log.info("查找{}的商品为：{}",userInfoVo.getId(),goodsList);
        return goodsList;
    }
}
